import { Module, VuexModule, Mutation, Action } from 'vuex-module-decorators';
import postApi from '../api/post/index';
import QueryConstants from 'src/constants/query';
import { Pagination } from 'src/types/pagination';
import { QueryPageParams } from 'src/types/requestParam';
import { HTTPResult } from 'src/types/httpResult';
import { Article } from 'src/models/article';
import { localStorage } from 'src/adapter/localstorage';
import { Console } from 'console';
const getDefaultListData = () => {
  return {
    data: [] as Article[],
    pagination: QueryConstants.PaginationConst as Pagination
  };
};

@Module<Article>({
  stateFactory: true,
  namespaced: true
  // name: 'comment',namespaced: false,
  // dynamic:true,
  // store:store,
})
export default class ArticleStateModule extends VuexModule {
  //数据获取中
  fetching = false as boolean;

  //文章列表
  articles = getDefaultListData();

  detail = null as Article | null;
  //
  articlesByTag = getDefaultListData();

  articlesByCategory = getDefaultListData();

  articlesByKeyword = getDefaultListData();

  archive = getDefaultListData();

  likeHistory: number[] = [];

  @Mutation
  SET_TOTAL(total: number) {
    this.articles.pagination.total = total;
  }

  @Mutation
  SET_FETCHING(fetching: boolean) {
    this.fetching = fetching;
  }

  @Mutation
  SET_DETIAL(post: Article) {
    this.detail = post;
  }

  @Mutation
  SET_LIST_DATA(articles: Article[]) {
    this.articles.data = articles;
  }

  @Mutation
  SET_PAGINATION(pagination: Pagination) {
    this.articles.pagination = pagination;
  }

  @Mutation
  APPEND_LIST_DATA(articles: Article[]) {
    this.articles.data = this.articles.data.concat(articles);
  }

  @Mutation
  SET_CATEGORY_LIST_DATA(articles: Article[]) {
    this.articlesByCategory.data = articles;
  }
  @Mutation
  APPEND_CATEGORY_DATA(articles: Article[]) {
    this.articlesByCategory.data = this.articlesByCategory.data.concat(
      articles
    );
  }

  @Mutation
  SET_CATEGORY_PAGINATION(pagination: Pagination) {
    this.articlesByCategory.pagination = pagination;
  }
  @Mutation
  SET_TAG_LIST_DATA(articles: Article[]) {
    this.articlesByTag.data = articles;
  }

  @Mutation
  APPEND_TAG_DATA(articles: Article[]) {
    this.articlesByTag.data = this.articlesByTag.data.concat(articles);
  }
  @Mutation
  SET_TAG_PAGINATION(pagination: Pagination) {
    this.articlesByTag.pagination = pagination;
  }

  @Mutation
  SET_LIKEHISTORY(history: number[]) {
    this.likeHistory = history;
  }

  @Mutation
  LIKE_POST(id: number) {
    if (this.likeHistory.find(n => n == id) == null) {
      this.likeHistory.push(id);
      localStorage.setItem('ARTICLE_LIKE_HISTORY', this.likeHistory);
    }
  }

  @Action({ rawError: true })
  async FetchList(params: QueryPageParams) {
    params = { ...QueryConstants.PaginationConst, ...params };

    this.context.commit('SET_FETCHING', true);

    const res = await postApi.getArtAll(params);
    if (params.page == 1) {
      this.context.commit('SET_LIST_DATA', res.data.list);
    } else {
      this.context.commit('APPEND_LIST_DATA', res.data.list);
    }
    this.context.commit('SET_PAGINATION', res.data.pagination);
    this.context.commit('SET_FETCHING', false);
  }

  @Action({ rawError: true })
  async ListByCategory(params: QueryPageParams | any) {
    params = { ...QueryConstants.PaginationConst, ...params };
    const { category } = params;

    this.context.commit('SET_FETCHING', true);

    const res = await postApi.listByCategory(params, category);
    if (params.page == 1) {
      this.context.commit('SET_CATEGORY_LIST_DATA', res.data.list);
    } else {
      this.context.commit('APPEND_CATEGORY_DATA', res.data.list);
    }
    this.context.commit('SET_CATEGORY_PAGINATION', res.data.pagination);

    this.context.commit('SET_FETCHING', false);
  }

  @Action({ rawError: true })
  async ListByTag(params: QueryPageParams | any) {
    params = { ...QueryConstants.PaginationConst, ...params };

    const { tag } = params;
    this.context.commit('SET_FETCHING', true);

    const res = await postApi.listByTag(params, encodeURIComponent(tag));
    if (params.page == 1) {
      this.context.commit('SET_TAG_LIST_DATA', res.data.list);
    } else {
      this.context.commit('APPEND_TAG_DATA', res.data.list);
    }
    this.context.commit('SET_TAG_PAGINATION', res.data.pagination);

    this.context.commit('SET_FETCHING', false);

    // this.context.commit('SET_TOTAL', res.data.pagination.total);
  }

  @Action({ rawError: true })
  async GetDetail(id: number) {
    // this.context.commit('SET_FETCHING', true);
    const res = await postApi.getDetail(id);
    this.context.commit('SET_DETIAL', res.data);
    // this.context.commit('SET_FETCHING', false);
  }

  @Action({ rawError: true })
  async Like(id: number): Promise<any> {
    this.context.commit('SetPosting', true);
    const res = (await postApi.likePost(id).catch()) as any;
    this.context.commit('SetPosting', false);
    return new Promise((resolve, reject) => {
      if (res.success) {
        console.log(res);
        this.context.commit('LIKE_POST', id);
        resolve(res.data);
      } else reject(false);
    });
  }
}
